package de.bastian.hinterleitner.adaptersviews;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import de.bastian.hinterleitner.database.DataHelper;
import de.bastian.hinterleitner.tools.Entry;
import de.bastian.hinterleitner.tools.Helper;
import de.hijacksoft.wyc.R;

/**
 *
 * @author basti
 */
public abstract class CategoryAdapter extends BasicAdapter{
    
    double max = 0;
    
    public CategoryAdapter(Context c, ArrayList<Entry> e){
    	entries = new ArrayList<Entry>();
        context = c;
        max = 0;
        DataHelper dh = DataHelper.getDataHelper(c);
        for(Entry en : e){
            if(Math.abs(en.getValue())>max)max = Math.abs(en.getValue());
            en.setDrawable(dh.getCategoryDrawable(en.getName()));
            entries.add(en);
        }
    }

    @Override
    public void setItems(ArrayList<Entry> list){
    	entries = new ArrayList<Entry>();
        max = 0;
        DataHelper dh = DataHelper.getDataHelper(context);
        for(Entry en : list){
        	if(Math.abs(en.getValue())>max)max = Math.abs(en.getValue());
        	en.setDrawable(dh.getCategoryDrawable(en.getName()));
            entries.add(en);
        }
        notifyDataSetChanged();
    }
    
    public abstract void buttonClicked(int index);
    
    @Override
    public View getView(final int i, View view, ViewGroup vg) {
        if (view == null) {
            LayoutInflater inflator = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.list_categorylist, null);
        }
        
        double val = entries.get(i).getValue();
        
        Button button = ((Button)view.findViewById(R.id.list_image_icon));
        button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				buttonClicked(i);
			}
        });
        
        if(entries.get(i).getDrawable() != null){
	        Drawable img = entries.get(i).getDrawable();
	        img.setBounds( 0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight() );
	        button.setCompoundDrawables(img, null, null, null);
        }
        
        TextView value = ((TextView)view.findViewById(R.id.list_text_value));
        ProgressBar bar = ((ProgressBar)view.findViewById(R.id.list_bar));
        
        ((TextView)view.findViewById(R.id.list_text_name)).setText(entries.get(i).getName());
        
        bar.setMax(100);
        bar.setProgress(Math.abs((int)(entries.get(i).getValue()*100/max)));
        
        value.setText(Helper.getValString(val)+" "+context.getString(R.string.currency));
        
        if(val<0){
            value.setTextColor(0xffdd3333);
        }else{
            value.setTextColor(0xff33aa33);
        }
        
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        max = 0;
        for(Entry e : entries){
        	if(Math.abs(e.getValue())>max)max = Math.abs(e.getValue());
        }
    }
}
